package com.wzz.dao.impl;

import com.wzz.dao.UserDao;
import com.wzz.entity.User;
import com.wzz.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImplement implements UserDao {
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    @Override
    public void createUser(User user) {
        User userd = null;
        try {
            con = DBUtil.getConnection();
            String sql = "insert into user (name,nickname,password,sex,age,birthday) values (?,?,?,?,?,?);";
            ps = con.prepareStatement(sql);
            ps.setString(1,user.getName());
            ps.setString(2,user.getNickName());
            ps.setString(3,user.getPassword());
            ps.setString(4,user.getSex());
            ps.setInt(5,user.getAge());
            ps.setString(6,user.getBirthday());

            int result = ps.executeUpdate();
            if(result == 1){
                System.out.println("创建新账号成功！");
            } else {
                System.out.println("创建失败！");
            }
        } catch (SQLException e){
            e.printStackTrace();
        } finally {
            try {
                DBUtil.closeConnection(con,ps,rs);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
//        System.out.println(userd.toString());
    }

    @Override
    public void login(String name, String password) {
//        Connection con = null;
//        PreparedStatement ps = null;
//        ResultSet rs = null;
        User userd = null;
        userd = new User();
        try {
            con = DBUtil.getConnection();
            String sql = "select * from user where name = ? and password = ?";
            ps = con.prepareStatement(sql);
            ps.setString(1,name);
            ps.setString(2,password);
            System.out.println(ps.toString());
            rs = ps.executeQuery();
            if(rs.next()){
                System.out.println("登录成功！");
                userd.setId(rs.getString("id"));
                userd.setPassword(rs.getString("password"));
                userd.setName(rs.getString("name"));
                userd.setNickName(rs.getString("nickname"));

                System.out.println(userd.toString());
            } else {
                System.out.println("用户名或密码错误！");
            }
        } catch (SQLException e){
            e.printStackTrace();
        } finally {
            try {
                DBUtil.closeConnection(con,ps,rs);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void loginOut(User user) {
        System.out.println("账号已登出！");
    }

    @Override
    public void userEdit(User user) {
        User userd = null;
        try {
            con = DBUtil.getConnection();
            String sql = "update user set name = ?,password = ?,nickname = ? where id = ?";
            ps = con.prepareStatement(sql);
            ps.setString(1,user.getName());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getNickName());
            ps.setString(4,user.getId());
            int result = ps.executeUpdate(sql);
            if(result == 1){
                System.out.println("编辑成功！");
            } else {
                System.out.println("编辑失败！");
            }
        } catch (SQLException e){
            e.printStackTrace();
        } finally {
            try {
                DBUtil.closeConnection(con,ps,rs);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void startCon(){

    }

    public void closeCon(){

    }
}
